package com.qf.shiro.base.realm;

import com.qf.shiro.base.entity.SysUser;
import com.qf.shiro.base.service.SysUserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

public class UserRealm extends AuthenticatingRealm {
    @Resource
    private SysUserService sysUserService;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //用户输入的用户名
        String username = (String) token.getPrincipal();
        SysUser userInfo = sysUserService.getUserInfo(username);
        if (ObjectUtils.isEmpty(userInfo)) {
            throw new UnknownAccountException();
        }
        if (userInfo.getDelStatus() != 0) {
            throw new LockedAccountException();
        }
        //  vo对象
        /*
         * Object principal,  从数据中获取的对向
         * Object hashedCredentials,  数据中的密码
         * ByteSource credentialsSalt,  加密盐
         * String realmName   用户名
         */
        return new SimpleAuthenticationInfo(userInfo, userInfo.getPassword(), null, username);
    }

}
